package com.kgc.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.kgc.pojo.Bill;
import com.kgc.service.BillService;
import com.kgc.utils.PageUtils;


/**
 * 订单控制器
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/view/bill")
public class BillController {

	@Autowired
	private BillService billService;

	/**
	 * 查看账单明细接口
	 * @param id
	 * @return
	 */
	@RequestMapping("/getBillById.htm")
	@ResponseBody
	public Object getBillById(Integer id){
		Map<String, Object> map = new HashMap<String, Object>();
		
		try {
			Bill bill = billService.getBillById(id); 
			map.put("result", bill);
			map.put("success", true);
			map.put("message", "查询订单成功,测试git,我：编写一个冲突");
		} catch (Exception e) {
			e.printStackTrace();
			map.put("success", false);
			map.put("message", "查询订单异常："+e);
		}

		return map;
	}

	/**
	 * 删除账单接口
	 * @param id
	 * @return
	 */
	@RequestMapping("/deleteBill.htm")
	@ResponseBody
	public Object deleteBill(Integer id){
		Map<String, Object> map = new HashMap<String, Object>();

		try {
			int count = billService.deleteBill(id);

			map.put("success", true);
			map.put("message", "删除订单成功");
		} catch (Exception e) {
			e.printStackTrace();
			map.put("success", false);
			map.put("message", "删除订单异常："+e);
		}

		return map;
	}

	/**
	 * 插入或者更新账单接口
	 * @param bill
	 * @return
	 */
	@RequestMapping("/addOrUpdateBill.htm")
	@ResponseBody
	public Object addOrUpdateBill(Bill bill){
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			if(bill.getId() != null){//修改订单
				billService.updateBill(bill);
				map.put("message", "添加修改成功");
			}else{//添加订单
				billService.addBill(bill);
				map.put("message", "添加订单成功");
			}

			map.put("success", true);
		} catch (Exception e) {
			e.printStackTrace();
			map.put("success", false);
			map.put("message", "订单添加或修改异常："+e);
		} 
		return map;
	}

	/**
	 * 获取订单记录数接口-行数
	 * @param productName
	 * @param providerId
	 * @param isPayment
	 * @return
	 */
	@RequestMapping("/getBillListPageCount.htm")
	@ResponseBody
	public Object getBillListPageCount(String productName, Integer providerId , Integer isPayment){
		Map<String, Object> map = new HashMap<String, Object>();

		try {
			Map<String, Object> params = new HashMap<String, Object>();
			params.put("productName", productName);
			params.put("providerId", providerId);
			params.put("isPayment", isPayment);

			int count = billService.getBillListPageCount(params);

			map.put("success", true);
			map.put("result", count);
			map.put("message", "获取订单数量成功");
		} catch (Exception e) {
			e.printStackTrace();
			map.put("success", false);
			map.put("message", "获取订单数量异常："+e);
		}

		return map;
	}

	/**
	 * 获取账单信息-分页接口
	 * @param pageNum
	 * @param pageSize
	 * @param productName
	 * @param providerId
	 * @param isPayment
	 * @return
	 */
	@RequestMapping("/getBillListPage.htm")
	@ResponseBody
	public Object getBillListPage(Integer pageNum, Integer pageSize, 
			String productName, Integer providerId , Integer isPayment){
		Map<String, Object> map = new HashMap<String, Object>();

		try {
			Integer start = PageUtils.getStart(pageNum, pageSize);
			Map<String, Object> params = new HashMap<String, Object>();
			params.put("start", start);
			params.put("pageSize", pageSize);
			params.put("productName", productName);
			params.put("providerId", providerId);
			params.put("isPayment", isPayment);

			List<Bill> list = billService.getBillListPage(params);
			map.put("success", true);
			map.put("result", list);
			map.put("message", "获取订单信息成功");
		} catch (Exception e) {
			e.printStackTrace();
			map.put("success", false);
			map.put("message", "获取订单信息异常："+e);
		}

		return map;
	}
}
